<template>
  <div id="goods">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="6">
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="$router.push('/goods/add')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="#" type="index" width="50" />
        <el-table-column prop="goods_name" label="商品名称" width="700px" />
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px" />
        <el-table-column prop="goods_weight" label="商品重量" width="100px" />
        <el-table-column prop="add_time" label="创建时间" width="160px" >
          <template v-slot="scope">
            {{ scope.row.add_time |  dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" />
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row.goods_id)"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import "@/assets/css/goods/goods.scss";

export default {
  data() {
    return {
      tableData: [],
      total: 0,
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      addVisible: false,
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.axios.get("/goods", {
        params: this.queryInfo,
      });
      if (res.meta.status == 200) {
        this.tableData = res.data.goods;
        this.total = res.data.total;
      } else {
        this.$message.error(res.meta.msg);
      }
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    async del(val) {
      const res = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (res == "confirm") {
        const { data: res } = await this.axios.delete(`goods/${val}`);
        if (res.meta.status == 200) {
          this.$message.success(res.meta.msg);
          this.getGoodsList();
        } else {
          this.$message.error(res.meta.msg);
        }
      } else if (res == "cancel") {
        this.$message.info("已取消删除");
      }
    },
  },
};
</script>

<style scoped></style>
